package com.example.chinesenation.Service.Impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chinesenation.Entity.Log;
import com.example.chinesenation.Entity.UserInterest;
import com.example.chinesenation.Exception.AuthenException;
import com.example.chinesenation.Mapper.UserDao;
import com.example.chinesenation.Entity.User;
import com.example.chinesenation.Redis.RedisService;
import com.example.chinesenation.Response.ResultType;
import com.example.chinesenation.Response.ServerResponse;
import com.example.chinesenation.Service.ILogService;
import com.example.chinesenation.Service.IUserInterestService;
import com.example.chinesenation.Service.UserService;
import com.example.chinesenation.Utils.Audience;
import com.example.chinesenation.Utils.JwtTokenUtil;
import com.example.chinesenation.VO.LoginData;
import com.example.chinesenation.VO.UserInterestJSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author sakuraFallingDown
 * @version 1.0
 * @date 2021/1/11 16:30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {


    private Logger logger= LoggerFactory.getLogger(getClass());

    @Autowired
    private ILogService logService;
    @Autowired
    private IUserInterestService userInterestService;

    @Autowired
    private RedisService redisService;

    @Override
    public ServerResponse<LoginData> login(String uid, String password, String browse,String ip,Audience audience) {
        QueryWrapper<User> qw=new QueryWrapper<>();
        if(uid==null||password==null) return ServerResponse.createErrorsResponse(ResultType.LOGINERROR);
        if(uid.length()!=11) return ServerResponse.createErrorsResponse((ResultType.LOGINERROR));

        qw.eq("uid",uid);
        qw.eq("password",password);
        User user=getOne(qw);
        if(user!=null){
            String token=JwtTokenUtil.createJWT(uid,audience);
            LoginData loginData=new LoginData();
            loginData.setToken(token);
            loginData.setUname(user.getUname());
            logService.saveLog(browse,ip,uid);
            return ServerResponse.createSuccessResponse("登录成功",loginData);
        }else{
            return ServerResponse.createErrorsResponse(ResultType.LOGINERROR);
        }
    }
    @Override
    public ServerResponse<String> register(User user) {
        if (user==null) return ServerResponse.createErrorsResponse(ResultType.LOGINERROR);
        String uid=user.getUid();
        String password=user.getPassword();
        if(uid==null||password==null) return ServerResponse.createErrorsResponse(ResultType.LOGINERROR);
        if(uid.length()!=11) return ServerResponse.createErrorsResponse((ResultType.LOGINERROR));
        if(save(user)){
            redisService.initUserInterest(uid);//初始化用户的兴趣矩阵
            return ServerResponse.createSuccessResponse("注册成功");
        }else{
            return ServerResponse.createSuccessResponse("注册失败");
        }
    }

    @Override
    public ServerResponse<String> update(User user) {
        long time=System.currentTimeMillis();
        if (user==null) return ServerResponse.createErrorsResponse(ResultType.LOGINERROR);
        if(updateById(user)){
            long time1=System.currentTimeMillis()-time;
            logger.info(""+time1);
            return ServerResponse.createSuccessResponse("注册成功");
        }else{
            return ServerResponse.createSuccessResponse("注册失败");
        }
    }

    @Override
    public User findUserButNotUidAndPassword(String uid,String token) {
        User user=getById(uid);
        if(user==null) throw new AuthenException(ResultType.LOGINERROR);
        user.setUid(token);
        user.setPassword(null);
        return user;
    }
}
